/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       clouds.frag
 * Author:     karooolek
 * Created on: 2010-01-06
 *
 **********************************************************************************************************************/

STRINGIZE(

varying vec2 position;

uniform sampler2D texture;
uniform float density;

uniform vec4 sunColor;
uniform vec3 sunDirection;
uniform float penetration;


void main()
{
    // cloud color
    vec4 color = texture2D(texture, position);
    
    // discard intensity below 1%
    if(color.a < 0.01)
    {
        discard;
    }

    // calc cloud thickness
    vec2 dir = -penetration * sunDirection.xy;
    float thick = texture2D(texture, position + 0.25 * dir).a;
    thick += texture2D(texture, position + 0.50 * dir).a;
    thick += texture2D(texture, position + 0.75 * dir).a;
    thick += texture2D(texture, position + dir).a;
    thick *= 0.25;
    
    gl_FragColor = sunColor * color * clamp(1.0 - density * thick, 0.0, 1.0);
    gl_FragColor.a = color.a;
}

) // STRINGIZE
